<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\service\AdminRomService;
use app\common\controller\Backend;
use \think\facade\View;
use \think\facade\Db;

/**
 * 后台主页控制器
 * @author 牧羊人
 * @since: 2020/6/30
 * Class Index
 * @package app\admin\controller
 */
class Index extends Backend
{
    /**
     * 初始化方法
     * @author 牧羊人
     * @since: 2020/6/30
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\admin\model\Admin();
    }

    /**
     * 后台主入口
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @since: 2020/6/30
     * @author 牧羊人
     */
    public function index()
    {
        // 取消模板布局
        $this->app->view->layout(false);

        // 获取导航菜单
        $adminRomService = new AdminRomService();
        $menuList = $adminRomService->getPermissionList($this->adminId);
        View::assign("menuList", $menuList);
        return $this->render();
    }

    /**
     * 欢迎页
     * @author 牧羊人
     * @since: 2020/6/30
     */
    public function main()
    {
        // 取消模板布局
        $data = [];
        $wh = [];
        $wh[] = ['mark','=',1];
        $wh_mo = [];
        $wh_mo[] = ['mark','=',1];
        $admin_id = session("adminId");
        if($admin_id != 1){   
            $data['d_compay'] = Db::name("of_company")->where($wh)->whereDay('create_time')->count();
            $wh_or = [];
            $wh_or[] = ["service_id","=",$admin_id];
            $wh_or[] = ["agent_id","=",$admin_id];
            $wh_or[] = ["peer_id","=",$admin_id];
            $arr_ids = Db::name("of_company")->where($wh)->where(function($query) use($wh_or){$query->whereOr($wh_or);})->column("id");            
            $wh[] = ['company_id', 'in', $arr_ids];
            $pa_ids = Db::name("of_patent")->where($wh)->column("id");
            $wh_mo[] = ['patent_id','in',$pa_ids];

            $data['total_compay'] = count($arr_ids);
        }else{
            $data['total_compay'] = Db::name("of_company")->where($wh)->count();
            $data['d_compay'] = Db::name("of_company")->where($wh)->whereDay('create_time')->count();
        }

        $data['pay_total'] = Db::name("of_patent_monitor")->where($wh_mo)->count();
        $data['pay_num'] = Db::name("of_patent_monitor")->where($wh_mo)->whereTime('pay_time', '<', date("Y-m-d",strtotime("+1 month")))->whereTime('pay_time', '>=', time())->count();
        $data['expire_num'] = Db::name("of_patent_monitor")->where($wh_mo)->whereTime('pay_time', '<', time())->count();

        $data['d_patent'] = Db::name("of_patent")->whereDay('create_time')->where($wh)->count();
        $data['d_project'] = Db::name("of_project")->whereDay('create_time')->where($wh)->count();
        $data['d_trademark'] = Db::name("of_trademark")->whereDay('create_time')->where($wh)->count();
        $data['d_copyright'] = Db::name("of_copyright")->whereDay('create_time')->where($wh)->count();

        $data['patent'] = Db::name("of_patent")->where($wh)->count();
        $data['project'] = Db::name("of_project")->where($wh)->count();
        $data['trademark'] = Db::name("of_trademark")->where($wh)->count();
        $data['copyright'] = Db::name("of_copyright")->where($wh)->count();
        View::assign("data", $data);

        $pa_list = Db::name("of_patent")->field("count(*) num,from_unixtime(create_time,'%Y%m') as ct")->where($wh)->group("ct")->order("ct desc")->select()->toArray();
        $pa_list = $this->getArr($pa_list);
        View::assign("pa_list", implode(",",$pa_list));

        $co_list = Db::name("of_copyright")->field("count(*) num,from_unixtime(create_time,'%Y%m') as ct")->where($wh)->group("ct")->order("ct desc")->select()->toArray();
        $co_list = $this->getArr($co_list);
        View::assign("co_list", implode(",",$co_list));

        $tr_list = Db::name("of_trademark")->field("count(*) num,from_unixtime(create_time,'%Y%m') as ct")->where($wh)->group("ct")->order("ct desc")->select()->toArray();
        $tr_list = $this->getArr($tr_list);
        View::assign("tr_list", implode(",",$tr_list));

        $pr_list = Db::name("of_project")->field("count(*) num,from_unixtime(create_time,'%Y%m') as ct")->where($wh)->group("ct")->order("ct desc")->select()->toArray();
        $pr_list = $this->getArr($pr_list);
        View::assign("pr_list", implode(",",$pr_list));

        $missing_order = Db::name("of_patent")->alias("p")
                            ->join("of_patent_monitor pm","pm.patent_id=p.id","LEFT")
                            ->where("p.status = 2 AND p.issue_date <> '' AND pm.id IS NULL")
                            ->count();
        View::assign("missing_order",$missing_order);

        $max1 = max($pa_list)>max($co_list)? max($pa_list):max($co_list);
        $max2 = max($tr_list)>max($pr_list)? max($tr_list):max($pr_list);
        $max = $max1>$max2?$max1:$max2;
        $max = ceil($max/10)*10;
        $interval = $max/5;
        View::assign("max", $max);
        View::assign("interval", $interval);
        
        $date = $this->getArr('',true);
        View::assign("date", implode(",",$date));

        $this->app->view->layout(false);
        return $this->render();
    }

    public function getArr($list='',$date=false)
    {
        $data = [];
        for ($i=0; $i < 12; $i++) { 
            $da = date("Ym",strtotime("-$i month",time()));            
            if(!$date){
                $flag = true;
                foreach ($list as $key => $value) {
                    if(!empty($value) && $da == $value['ct']){
                        array_unshift($data,$value['num']);
                        $flag = false;
                        break;
                    }
                }
                if($flag){
                    array_unshift($data,0);
                }
            }else{
                array_unshift($data,$da);
            }
        }
        return $data;
    }

    /**
     * 个人中心
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author 牧羊人
     * @since 2020/7/11
     */
    public function userInfo()
    {
        if (IS_POST) {
            // 参数
            $param = request()->param();
            // 真实姓名
            $realname = trim($param['realname']);
            // 邮箱
            $email = trim($param['email']);
            // 个人简介
            $intro = trim($param['intro']);
            // 街道地址
            $address = trim($param['address']);
            // 联系电话
            $mobile = trim($param['mobile']);
            $data = [
                'id' => $this->adminId,
                'realname' => $realname,
                'email' => $email,
                'intro' => $intro,
                'address' => $address,
                'mobile' => $mobile,
            ];
            $result = $this->model->edit($data);
            if (!$result) {
                return message("信息保存失败", false);
            }
            return message();
        }
        $adminMod = new \app\admin\model\Admin();
        $adminInfo = $adminMod->getInfo($this->adminId);
        View::assign("adminInfo", $adminInfo);
        return $this->render();
    }

    /**
     * 更新密码
     * @return array
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     * @since 2020/7/11
     * @author 牧羊人
     */
    public function updatePwd()
    {
        // 参数
        $param = request()->param();
        // 原密码
        $oldPassword = trim($param['oldPassword']);
        if (!$oldPassword) {
            return message("原密码不能为空", false);
        }
        // 新密码
        $newPassword = trim($param['newPassword']);
        if (!$newPassword) {
            return message("新密码不能为空", false);
        }
        // 确认密码
        $rePassword = trim($param['rePassword']);
        if (!$rePassword) {
            return message("确认密码不能为空", false);
        }
        if ($newPassword != $rePassword) {
            return message("两次输入的密码不一致", false);
        }
        if (get_password($oldPassword . $this->adminInfo['username']) != $this->adminInfo['password']) {
            return message("原始密码不正确", false);
        }
        // 设置新密码
        $data = [
            'id' => $this->adminId,
            'password' => get_password($newPassword . $this->adminInfo['username']),
        ];
        $adminMod = new \app\admin\model\Admin();
        $result = $adminMod->edit($data);
        if (!$result) {
            return message("修改失败", false);
        }
        return message("修改成功");
    }

}